package com.gtlab1207.br_awareness.services;


import ch.ethz.ssh2.Connection;
import com.gtlab1207.br_awareness.common.PageUtil;
import com.gtlab1207.br_awareness.models.dao.LogHostDao;
import com.gtlab1207.br_awareness.models.domain.LogHost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扫描日志主机信息业务层
 */
@Service
public class LogHostService {
    @Autowired
    private LogHostDao logHostDao;

    /**
     * 新增主机信息
     * @param logHost
     * @return
     */
    @Transactional
    public Integer insertLogHost(LogHost logHost){
        try {
            Connection conn = new Connection(logHost.getIpAddress());
            conn.connect();
            //进行身份认证
            boolean isAuthenticated = conn.authenticateWithPassword(
                    logHost.getUser(),logHost.getPassword());
            if (!isAuthenticated)
                return -1;
        } catch (IOException e) {
            return 0;
        }
        return logHostDao.insertLogHost(logHost);
    }

    /**
     * 获取主机信息列表
     * @param params
     * @return
     */
    public Map<String, Object> selectLogHost(Map params,Integer pageSize){
        Map<String, Object> map = new HashMap<>();
        List<LogHost> list = logHostDao.selectLogHost(params);
        Integer count = logHostDao.getCount(params);
        Integer pageCounts = PageUtil.setPageInfoPageSize(count, pageSize);
        map.put("data", list);//当前页数据
        map.put("total", list.size());//当前页数据总数
        map.put("all",count);//满足查询条件的数据总数
        map.put("pageCounts",pageCounts);//满足查询条件的数据总页数
        return map;
    }

    /**
     * 更新主机信息
     * @param logHost
     * @return
     */
    @Transactional
    public Integer updateLogHost(LogHost logHost){
        int result = 0;
        if(logHost.getId()!=null)
            result = logHostDao.updateLogHost(logHost);
        return result;
    }

    /**
     * 删除主机信息
     * @param id
     * @return
     */
    @Transactional
    public Integer deleteLogHost(Integer id){
        return logHostDao.deleteLogHost(id);
    }

    /**
     * 主机启用日志扫描
     * @param id
     * @return
     */
    @Transactional
    public Integer hostActivate(Integer id){
        return logHostDao.hostActivate(id);
    }

    /**
     * 主机关闭日志扫描
     * @param id
     * @return
     */
    @Transactional
    public Integer hostDeactivate(Integer id){
        return logHostDao.hostDeactivate(id);
    }

    /**
     * 根据主机ip获取主机信息
     * @param ipAddress
     * @return
     */
    public LogHost getLogHostByIp(String ipAddress){
        return logHostDao.getLogHostByIp(ipAddress);
    }

    /**
     * 获取所有启用扫描日志的主机
     * @return
     */
    public List<LogHost> getActivatedLogHost(){
        return logHostDao.getActivatedLogHost();
    }

    /**
     * 更新上次扫描时间
     * @param id
     * @return
     */
    @Transactional
    public Integer updateLastScanTime(Integer id){
        return logHostDao.updateLastScanTime(id);
    }
}
